package com.totcy.atom.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.totcy.atom.R;
import com.totcy.atom.bean.GankTypeBean;
import com.totcy.atom.utils.glide.GlideUtil;
import com.totcy.atom.view.activity.PictureBrowsingActivity;
import com.totcy.baselibrary.adapter.BaseLoadMoreAdapter;
import com.totcy.baselibrary.adapter.BaseViewHolder;
import com.totcy.baselibrary.utils.DisplayUtil;
import com.totcy.baselibrary.utils.ScreenUtil;

import java.util.List;


/**
 * description: gank文字数据适配器 android或者ios干货
 * author: Totcy
 * date: 2017/11/3 16:33
 * update: 2017/11/3
 */

public class GankTextAdapter extends BaseLoadMoreAdapter<GankTypeBean, BaseViewHolder, BaseViewHolder> {

    private View header;
    private int height;
    private int width;


    public GankTextAdapter(Context mContext, List<GankTypeBean> mDatas, View header) {
        super(mContext, mDatas);
        this.header = header;
    }

    public GankTextAdapter(Context mContext, List<GankTypeBean> mDatas) {
        super(mContext, mDatas);
    }

    @Override
    protected BaseViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
        width = ScreenUtil.getScreenWidth(mContext);
        height = DisplayUtil.dip2px(mContext, 250);

        return new BaseViewHolder(getItemView(R.layout.item_gank_text, parent));
    }

    @Override
    protected BaseViewHolder onCreateHeaderViewHolder(ViewGroup parent, int viewType) {
        return new BaseViewHolder(header);
    }

    @Override
    protected void onBindItemViewHolder(BaseViewHolder holder, final int position) {
        holder.setText(R.id.tv_desc, mDatas.get(position).getDesc());
        if (mDatas.get(position).getImages() != null && mDatas.get(position).getImages().size() > 0) {
            String url = mDatas.get(position).getImages().get(0) + "?imageView2/0/w/" + width + "/h/" + height;

            ImageView view = holder.getView(R.id.iv_gank);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("position", 0);
                    bundle.putStringArrayList("urlList", mDatas.get(position).getImages());
                    mContext.startActivity(new Intent(mContext, PictureBrowsingActivity.class).putExtras(bundle));
                }
            });

            Glide.with(mContext).asBitmap().load(url)
                    //过渡动画
                    //.transition(DrawableTransitionOptions.withCrossFade())
                    //V4 写法
                    .apply(new RequestOptions()
                            .placeholder(R.drawable.img_default)
                            .error(R.drawable.img_default)
                            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                            .centerInside())
                    .into(view);

            holder.setVisible(R.id.iv_gank, true);

        } else {
            holder.setVisible(R.id.iv_gank, false);
        }


    }

    @Override
    protected void onBindHeaderViewHolder(BaseViewHolder holder) {
    }

    @Override
    public boolean hasHeader() {
        return header != null;
    }
}
